import { requestGetUserMsg } from '../../request/index.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentIndex: 0,
    collectionStoreFood: [],
    storeMsg: {
      // store_id: 1,
      // store_img_url: "http://q6zagsjbu.bkt.clouddn.com/1001.jpg",
      // storeName: "海哥汉堡",
      // store_announce: "祝所有的客人喜提和谐号",
      // score: 2.2,
      // choose_collect: false
    },
    leftCate: [],
    rightGoods: [],
    foodCate: [
      //   {
      //   good_id: "0",
      //   good_name: "汉堡主食",
      //   good_pic: null,
      //   good_level: 1,
      //   isActive: false,
      //   children: [{
      //     food_id: "901",
      //     food_name: "香辣鸡腿堡",
      //     food_msg: "主要原料:鸡肉",
      //     storeName: "海哥汉堡",
      //     food_pic: "http://q6zagsjbu.bkt.clouddn.com/hamburger.jpg",
      //     store_img_url: "http://q6zagsjbu.bkt.clouddn.com/1001.jpg",
      //     food_detail: "/pages/good_detail/good_detail",
      //     food_level: 2,
      //     food_sales: 28,
      //     food_price: 12
      //   },
      //   {
      //     food_id: "902",
      //     food_name: "藤椒鸡腿堡",
      //     food_msg: "主要原料:鸡肉",
      //     food_pic: "http://q6zagsjbu.bkt.clouddn.com/hamburger.jpg",
      //     store_img_url: "http://q6zagsjbu.bkt.clouddn.com/1001.jpg",
      //     food_detail: "/pages/good_detail/good_detail",
      //     food_level: 2,
      //     storeName: "海哥汉堡",
      //     food_sales: 220,
      //     food_price: 24
      //   }
      //   ]
      // },
      // {
      //   good_id: "1",
      //   good_name: "美味小吃",
      //   good_pic: null,
      //   good_level: 1,
      //   isActive: false,
      //   children: [{
      //     food_id: "1001",
      //     food_name: "香辣鸡翅",
      //     food_msg: "香辣 值得品尝",
      //     food_pic: "http://q6zagsjbu.bkt.clouddn.com/hamburger.jpg",
      //     store_img_url: "http://q6zagsjbu.bkt.clouddn.com/1001.jpg",
      //     storeName: "海哥汉堡",
      //     food_detail: "/pages/good_detail/good_detail",
      //     food_level: 2,
      //     food_sales: 28,
      //     food_price: 12
      //   }, {
      //     food_id: "1002",
      //     food_name: "劲爆鸡米花",
      //     food_msg: "采用西餐标准规格鸡腿肉,辣味腌制",
      //     storeName: "海哥汉堡",
      //     food_pic: "http://q6zagsjbu.bkt.clouddn.com/hamburger.jpg",
      //     store_img_url: "http://q6zagsjbu.bkt.clouddn.com/1001.jpg",
      //     food_detail: "/pages/good_detail/good_detail",
      //     food_level: 2,
      //     food_sales: 28,
      //     food_price: 12
      //   },

      //   ]
      // },
      // {
      //   good_id: "2",
      //   good_name: "汉堡套餐",
      //   good_pic: null,
      //   good_level: 1,
      //   isActive: false,
      //   children: [
      //     {
      //       food_id: "2001",
      //       food_name: "管饱单人餐",
      //       food_msg: "鸡腿堡+墨西哥鸡肉卷+中杯渴了",
      //       food_pic: "http://q6zagsjbu.bkt.clouddn.com/hamburger.jpg",
      //       store_img_url: "http://q6zagsjbu.bkt.clouddn.com/1001.jpg",
      //       food_detail: "/pages/good_detail/good_detail",
      //       storeName: "海哥汉堡",
      //       food_level: 2,
      //       food_sales: 66,
      //       food_price: 35
      //     },
      //     {
      //       food_id: "2002",
      //       food_name: "经典单人餐",
      //       food_msg: "鸡腿堡+香辣鸡翅+中杯可乐",
      //       food_pic: "http://q6zagsjbu.bkt.clouddn.com/hamburger.jpg",
      //       store_img_url: "http://q6zagsjbu.bkt.clouddn.com/1001.jpg",
      //       food_detail: "/pages/good_detail/good_detail",
      //       storeName: "海哥汉堡",
      //       food_level: 2,
      //       food_sales: 66,
      //       food_price: 35
      //     }
      //   ]
      // },
      // {
      //   good_id: "3",
      //   good_name: "多人套餐",
      //   good_pic: null,
      //   good_level: 1,
      //   isActive: false,
      //   children: [
      //     {
      //       food_id: "3001",
      //       food_name: "经典双人餐",
      //       food_msg: "鸡腿堡2个+上校鸡块2份+中杯可乐2杯",
      //       food_pic: "http://q6zagsjbu.bkt.clouddn.com/hamburger.jpg",
      //       store_img_url: "http://q6zagsjbu.bkt.clouddn.com/1001.jpg",
      //       food_detail: "/pages/good_detail/good_detail",
      //       storeName: "海哥汉堡",
      //       food_level: 2,
      //       food_sales: 66,
      //       food_price: 55
      //     },
      //     {
      //       food_id: "3002",
      //       food_name: "全家桶套餐",
      //       food_msg: "牛肉煲+虾堡+蜜汁烤鸡腿+孜然鸡柳+可乐三杯",
      //       food_pic: "http://q6zagsjbu.bkt.clouddn.com/hamburger.jpg",
      //       store_img_url: "http://q6zagsjbu.bkt.clouddn.com/1001.jpg",
      //       food_detail: "/pages/good_detail/good_detail",
      //       storeName: "海哥汉堡",
      //       food_level: 2,
      //       food_sales: 66,
      //       food_price: 90
      //     }
      //   ]
      // },
      // {
      //   good_id: "4",
      //   good_name: "饮品凉茶",
      //   good_pic: null,
      //   good_level: 1,
      //   isActive: false,
      //   children: [
      //     {
      //       food_id: "4001",
      //       food_name: "中杯可乐",
      //       food_msg: "请备注热饮/冷饮",
      //       food_pic: "http://q6zagsjbu.bkt.clouddn.com/hamburger.jpg",
      //       store_img_url: "http://q6zagsjbu.bkt.clouddn.com/1001.jpg",
      //       food_detail: "/pages/good_detail/good_detail",
      //       storeName: "海哥汉堡",
      //       food_level: 2,
      //       food_sales: 66,
      //       food_price: 55
      //     },
      //     {
      //       food_id: "4002",
      //       food_name: "中杯雪碧",
      //       food_msg: "请备注热饮/冷饮",
      //       food_pic: "http://q6zagsjbu.bkt.clouddn.com/hamburger.jpg",
      //       store_img_url: "http://q6zagsjbu.bkt.clouddn.com/1001.jpg",
      //       food_detail: "/pages/good_detail/good_detail",
      //       storeName: "海哥汉堡",
      //       food_level: 2,
      //       food_sales: 66,
      //       food_price: 55
      //     },
      //     {
      //       food_id: "4003",
      //       food_name: "中杯芬达",
      //       food_msg: "请备注热饮/冷饮",
      //       food_pic: "http://q6zagsjbu.bkt.clouddn.com/hamburger.jpg",
      //       store_img_url: "http://q6zagsjbu.bkt.clouddn.com/1001.jpg",
      //       food_detail: "/pages/good_detail/good_detail",
      //       storeName: "海哥汉堡",
      //       food_level: 2,
      //       food_sales: 66,
      //       food_price: 55
      //     },
      //   ]
      // }
    ]
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //1.获取店铺信息
    let allPage = getCurrentPages();
    let { store_id } = allPage[allPage.length-1].options
    console.log();
    this.getStoreMsg(store_id);
    // let {store_id} = allPage[1].options
    // console.log(store_id)

    //2.获取foodCate  根据店铺id查询
    this.getFoodCate(store_id);

    //3.判断是否被收藏过了
    this.checkCollection();
    //获取当前页面传入的参数  店铺id

    // //7.从本地获取collectionStore
    // let collectionStoreFood = wx.getStorageSync('collectionStoreFood') || [];
    // if (collectionStoreFood.length > 0) {
    //   let index = collectionStoreFood.findIndex(v => v.store_id = storeMsg.store_id);
    //   // 索引不为-1证明找到了
    //   if(index>-1){
    //     // 把获取到的收藏状态赋值到本地
    //     collectionStoreFood[index].choose_collect = storeMsg.choose_collect;
    //   }
    //   this.setData({storeMsg});
    // }else{
    //   //本地没有商品/店铺收藏的信息  从服务器获取
    //   let {data} = requestGetUserMsg({url:"/getCollectionStoreFood"});
    //     console.log(data);
    // }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  handleItemTap: function (e) {
    //1.分类列表的索引
    var { index } = e.currentTarget.dataset;
    //2.修改选中状态
    let { leftCate } = this.data;
    var food_id;
    leftCate.forEach((v, i) => {
      // index == i ? v.isActive = v.isActive = true : v.isActive = false;
      if (index == i) {
        v.isActive = true;
        food_id = v.food_id;
      } else {
        v.isActive = false;
      }
    })
    this.setData({ leftCate });
    //3.获取右侧的数据信息
    this.getFoodByFoodId(food_id);

  },
  async  getFoodByFoodId(food_id) {
    var { data } = await requestGetUserMsg({ url: "/getFoodByFoodId", data: { food_id: food_id } });
    this.setData({ rightGoods: data });
  },
  // 店铺收藏
async  collect() {
    //获取用户信息
    var userInfo = wx.getStorageSync("userInfo");
    //判断是否登录
    this.checkLogin();
    //1.获取商品详情信息
    let { storeMsg } = this.data;
    // console.log(storeMsg)
    // 2.给收藏按钮取反
    storeMsg.choose_collect = !storeMsg.choose_collect;
    // 3.设置回到data中
    this.setData({ storeMsg: storeMsg, choosed: storeMsg.choose_collect })
    // 4.从内存中取出collectionStore   //收藏的集合
    let collectionStoreFood = wx.getStorageSync('collectionStoreFood') || [];
    //4.2 判断 收藏集合是否为空
    if (collectionStoreFood.length > 0) {
      // 根据商品id查询是否存在
      let index = collectionStoreFood.findIndex(v => v.store_id === storeMsg.store_id);
      // console.log(index)
      //如果存在替换,不存在添加
      index == -1 ? collectionStoreFood.push({ user_id: collectionStoreFood[0].user_id, store_id: storeMsg.store_id, food_id: null }) : collectionStoreFood[index].choose_collect = !collectionStoreFood[index].choose_collect;
    } else {
      //直接添加
      collectionStoreFood.push({ user_id: collectionStoreFood[0].user_id, store_id: storeMsg.store_id, food_id: null })
    }
    //5.把收藏商品设置到内存
    wx.setStorageSync('collectionStoreFood', collectionStoreFood);
    // 6.请求服务器 修改改变的信息
    // console.log(storeMsg.choose_collect);
    await requestGetUserMsg({ url: "/updateCollectionFoodStore?type=1&phoneNum=" + userInfo.phoneNum, method: "POST", data: { choose_collect:storeMsg.choose_collect,store_id:storeMsg.store_id } });

  },
  //获取店铺的信息
  async  getStoreMsg(store_id) {

    let nowStoreMsg_1 = await requestGetUserMsg({ url: "/getStoreMsgByStoreId", data: { store_id: store_id } });
    this.setData({ storeMsg: nowStoreMsg_1.data[0] });


    // //nowStoreMsg  上面的需要赋值的 店铺信息
    // let nowStoreMsg = this.data.storeMsg;
    // //1.先从本地缓存获取店铺信息  [] 
    // let storeMsg = wx.getStorageSync("storeMsg") || [];

    // //2.有店铺列表信息
    // if (storeMsg.length > 0) {
    //   //3.遍历店铺列表  获取当前店铺的信息
    //   storeMsg.forEach(v => v.store_id == store_id ? nowStoreMsg = v : '');
    //   //3.1 当前的店铺信息存在  赋值给店铺信息
    //   if (nowStoreMsg.length > 0) {
    //     this.setData({ storeMsg: nowStoreMsg });
    //   } else {
    //     //3.2 店铺信息不存在
    //     let nowStoreMsg_1 = await requestGetUserMsg({ url: "/getStoreMsgByStoreId", data: { store_id: store_id } });
    //     this.setData({ storeMsg: nowStoreMsg_1.data[0] });
    //     // storeMsg = [...storeMsg, ...nowStoreMsg_1.data];
    //   }
    // } else {
    //   //没有店铺列表信息  请求服务器获取店铺列表信息
    //   let nowStoreMsg_2 = await requestGetUserMsg({ url: "/getStoreMsgByStoreId", data: { store_id: store_id } });
    //   // console.log(nowStoreMsg_2.data[0])
    //   this.setData({ storeMsg: nowStoreMsg_2.data[0] });
    //   // storeMsg = [...storeMsg, ...nowStoreMsg_2.data];
    // }
    // //设置店铺信息到缓存
    // wx.setStorageSync("storeMsg", storeMsg);
  },


  //获取分类信息 和对应的商品信息
  async  getFoodCate(store_id) {
    // // 1.从缓存获取foodCate  商品信息
    // let foodCate = wx.getStorageSync('foodCate') || [];
    // //data中的分类
    // let nowFoodCate = this.data.foodCate;
    // let leftCate = this.leftCate;


    //2.根据商品id获取商品分类信息
    var { data } = await requestGetUserMsg({ url: "/getFoodCateByStoreId", data: { store_id: store_id } });
    //赋值给左边分类列表
    let leftCate = data;
    //3把左边分类列表的第一个按钮 的isActive=true 
    leftCate.forEach((v, i) => {
      i == 0 ? v.isActive = true : v.isActive = false;
    })
    // 4.设置参数
    this.setData({
      leftCate
    });
    // console.log(leftCate)


    //5.获取到分类id 
    let food_id = leftCate[0].food_id;
    //6.根据分类id 查询对应的 商品
    var { data } = await requestGetUserMsg({ url: "/getFoodByFoodId", data: { food_id: food_id } });
    this.setData({ rightGoods: data });
  },
  async  checkCollection() {
    //1.根据用户电话获取收藏的集合
    let userInfo = wx.getStorageSync('userInfo');
    //2.用户存在
    if (userInfo.phoneNum > 1) {
      //3.请求服务器, 获取收藏的店铺的集合   choose_collect   storeMsg
      var { data } = await requestGetUserMsg({ url: "/getCollectionStoreFood", data: { phoneNum: userInfo.phoneNum, index: 3 } });
      //4.获取店铺信息
      let storeMsg = this.data.storeMsg;
      data.forEach(v => v.store_id == storeMsg.store_id ? storeMsg.choose_collect = true : '');
      this.setData({ storeMsg });
      wx.setStorageSync("collectionStoreFood", data);
      // console.log(data);
    } else {
      //2.2用户不存在  啥也不用干
    }
  },
  checkLogin(){
    //0.判断用户是否登录并且绑定手机号
    let userInfo = wx.getStorageSync("userInfo") || [];
    if (userInfo.phoneNum == undefined) {
      wx.navigateTo({
        url: '/pages/my_center/my_center',
        success: (result) => {
          wx.showToast({
            title: '需要登录并且绑定手机号',
            image: '../../icon/jinggao.png',
            duration: 1500
          });
        }
      });
      return;
    }
  }
})